#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include "mypushbutton.h"
#include "chooselevelscene.h"
#include <QTimer>
//#include <QSoundEffect>
#include <QtMultimedia/QSound>

MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainScene)
{
    ui->setupUi(this);
    init();
    setConnect();

}

MainScene::~MainScene()
{
    delete ui;
}

void MainScene::init(){
    this->setFixedSize(320,588);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("老帮主带你翻金币");
    this->m_StartBtn = new MyPushButton(":/res/MenuSceneStartButton.png",":/res/MenuSceneStartButton.png",this);
    this->m_StartBtn->move(this->width()*0.5-m_StartBtn->width()*0.5,this->height()*0.7);
    this->m_pChooseLevelScene = new ChooseLevelScene(this);
}

void MainScene::setConnect(){
    connect(ui->actionQuit,&QAction::triggered,this,&MainScene::onAcitionQuitClicked);
    connect(this->m_StartBtn,&MyPushButton::clicked,this,&MainScene::onStartBtnClicked);
    connect(this->m_pChooseLevelScene,&ChooseLevelScene::chooseSceneBack,this,&MainScene::BackToMain);
}

void MainScene::paintEvent(QPaintEvent* ev){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
}

void MainScene::onAcitionQuitClicked(){
    this->close();
}

void MainScene::startChoose(){
    this->hide();
    m_pChooseLevelScene->show();
}

void MainScene::onStartBtnClicked(){
    this->m_StartBtn->zoom1();
    this->m_StartBtn->zoom2();
    QSound* startSound = new QSound(":/res/TapButtonSound.wav",this);
    startSound->play();
    QTimer::singleShot(500,this,&MainScene::startChoose);
}

void MainScene::BackToMain(){
    m_pChooseLevelScene->hide();
    this->show();
}
